package xin.marcher.module.lifecycle.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import xin.marcher.framework.common.util.data.DataCuttingUtil;
import xin.marcher.module.common.constants.construction.ProductConstants;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.enums.YesOrNoEnum;
import xin.marcher.module.common.exception.CommonErrorCodeEnum;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.lifecycle.converter.CategoryExpriWhiteConverter;
import xin.marcher.module.lifecycle.domain.dto.CategoryExpriWhiteDTO;
import xin.marcher.module.lifecycle.domain.entity.CategoryExpriWhiteDO;
import xin.marcher.module.lifecycle.domain.request.CategoryExpriWhiteRequest;
import xin.marcher.module.lifecycle.domain.response.CategoryExpriWhiteResponse;
import xin.marcher.module.lifecycle.mapper.CategoryExpriWhiteMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 考核白名单资源管理
 */
@Slf4j
@Repository
public class CategoryExpriWhiteRepository {

    @Resource
    private CategoryExpriWhiteMapper categoryExpriWhiteMapper;

    @Resource
    private CategoryExpriWhiteConverter categoryExpriWhiteConverter;

    /**
     * 添加考核白名单
     *
     * @param request
     * @return
     */
    public CategoryExpriWhiteResponse saveExpriWhite(CategoryExpriWhiteRequest request) {
        CategoryExpriWhiteDTO expriWhiteDTO = getExpriWhite(request.getCategoryId());

        if (Objects.nonNull(expriWhiteDTO)) {
            return new CategoryExpriWhiteResponse(Boolean.FALSE, "需要保存的记录已存在");
        }

        CategoryExpriWhiteDO categoryExpriWhiteDO = categoryExpriWhiteConverter.requestToEntity(request);
        categoryExpriWhiteDO.initCommon();

        int count = categoryExpriWhiteMapper.insert(categoryExpriWhiteDO);
        if (count <= 0) {
            throw new ProductBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        return new CategoryExpriWhiteResponse();
    }

    /**
     * 修改考核白名单
     *
     * @param request
     * @return
     */
    public CategoryExpriWhiteResponse updateExpriWhite(CategoryExpriWhiteRequest request) {
        CategoryExpriWhiteDTO expriWhiteDTO = getExpriWhite(request.getCategoryId());

        if (Objects.isNull(expriWhiteDTO)) {
            return new CategoryExpriWhiteResponse(Boolean.FALSE, "选择修改的记录不存在");
        }

        Long id = expriWhiteDTO.getId();

        CategoryExpriWhiteDO categoryExpriWhiteDO = categoryExpriWhiteConverter.requestToEntity(request);
        categoryExpriWhiteDO.setId(id);
        categoryExpriWhiteDO.initCommon();

        int count = categoryExpriWhiteMapper.updateById(categoryExpriWhiteDO);
        if (count <= 0) {
            throw new ProductBizException(CommonErrorCodeEnum.SQL_ERROR);
        }

        return new CategoryExpriWhiteResponse();
    }

    /**
     * 查询考核白名单
     *
     * @param categoryId
     * @return
     */
    public CategoryExpriWhiteDTO getExpriWhite(Integer categoryId) {
        LambdaQueryWrapper<CategoryExpriWhiteDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CategoryExpriWhiteDO::getCategoryId, categoryId);
        return categoryExpriWhiteConverter.entityToDTO(categoryExpriWhiteMapper.selectOne(queryWrapper));
    }

    /**
     * 分页查询出categoryIdList对应的白名单集合
     *
     * @param categoryIdList 品类ID集合
     * @return 处于白名单范围内的categoryIdList
     */
    public List<Integer> queryExpriWhiteByCategoryIdList(List<Integer> categoryIdList) {
        // 处于白名单范围内的categoryIdList
        List<Integer> whiteCategoryIdList = new ArrayList<>();
        // 一次最大查询200个数据，多个分页查询,这里做数据切割
        List<List<Integer>> splitList = DataCuttingUtil.dataCuttingString(categoryIdList, ProductConstants.QUERY_ITEM_MAX_COUNT);
        for (List<Integer> categoryIds : splitList) {
            LambdaQueryWrapper<CategoryExpriWhiteDO> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(CategoryExpriWhiteDO::getCategoryId, categoryIds);
            queryWrapper.eq(CategoryExpriWhiteDO::getDelFlag, YesOrNoEnum.YES.getCode());
            queryWrapper.eq(CategoryExpriWhiteDO::getActiveFlag, YesOrNoEnum.YES.getCode());
            // 分批次查询出categoryIdList对应的白名单集合
            List<CategoryExpriWhiteDO> categoryResultList = categoryExpriWhiteMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(categoryResultList)) {
                whiteCategoryIdList.addAll(categoryResultList.stream().map(CategoryExpriWhiteDO::getCategoryId).collect(Collectors.toList()));
            }
        }
        return whiteCategoryIdList;
    }

    /**
     * 分页查询考核白名单
     *
     * @param request
     * @return
     */
    public PageResult<CategoryExpriWhiteDTO> pageExpriWhite(CategoryExpriWhiteRequest request) {
        LambdaQueryWrapper<CategoryExpriWhiteDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(request.getCategoryId()), CategoryExpriWhiteDO::getCategoryId, request.getCategoryId());
        queryWrapper.eq(Objects.nonNull(request.getActiveFlag()), CategoryExpriWhiteDO::getActiveFlag, request.getActiveFlag());
        queryWrapper.like(Objects.nonNull(request.getCategoryName()), CategoryExpriWhiteDO::getCategoryName, request.getCategoryName());

        // 分页查询
        Page<CategoryExpriWhiteDO> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<CategoryExpriWhiteDO> pageResult = categoryExpriWhiteMapper.selectPage(page, queryWrapper);

        return categoryExpriWhiteConverter.converterPageResult(pageResult);
    }

}
